/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.persistence.hibernate;

import xperiment.metaphor.model.AbstractSpringJUnit;
import java.util.List;
import xperiment.metaphor.model.system.Message;
import xperiment.metaphor.persistence.filter.Expressions;
import xperiment.metaphor.persistence.filter.Query;

/**
 *
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 */
public class HibernateMessageDaoTest extends AbstractSpringJUnit {
    
    public HibernateMessageDaoTest(String testName) {
        super(testName);
    }            

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Test of retrieveByRecipient method, of class HibernateMessageDao.
     */
    public void testRetrieveByRecipient() throws Exception {
        logger.info("retrieveByRecipient");
        
        Query recipientQuery = Query.forClass(Message.class)
                .add(Expressions.eq("recipientId", 1L))
                .add(Expressions.eq("enabled", true));
        
        try {
            Message[] messages = new Message[] {
                new Message(1L, 1L, "Test"),
                new Message(2L, 1L, "Test"),
                new Message(3L, 1L, "Test"),
                new Message(4L, 1L, "Test"),
                new Message(5L, 1L, "Test"),
                new Message(6L, 1L, "Test"),
                new Message(7L, 1L, "Test")
            };
            for (int i = 0; i < messages.length; i++) {
                messages[i] = (Message) getDao().persist((Message) messages[i]);
            }

            List<Message> results = getDao().retrieve(recipientQuery);
            assertEquals(messages.length, results.size());

             Message[] additional = new Message[] {
                new Message(1L, 2L, "Test"),
                new Message(2L, 2L, "Test"),
                new Message(3L, 2L, "Test"),
                new Message(4L, 2L, "Test"),
                new Message(5L, 2L, "Test"),
                new Message(6L, 2L, "Test"),
                new Message(7L, 2L, "Test")
            };
            for (int i = 0; i < additional.length; i++) {
                additional[i] = (Message) getDao().persist((Message) additional[i]);
            }

            //Test that we are not picking up other peoples messages 
            results = getDao().retrieve(recipientQuery);
            assertEquals(messages.length, results.size());


            Message[] archived = new Message[] {
                new Message(1L, 1L, "Test"),
                new Message(2L, 1L, "Test"),
                new Message(3L, 1L, "Test"),
                new Message(4L, 1L, "Test"),
                new Message(5L, 1L, "Test"),
                new Message(6L, 1L, "Test"),
                new Message(7L, 1L, "Test")
            };
            for (int i = 0; i < additional.length; i++) {
                Message m = (Message) archived[i];
                m.setEnabled(false);
                archived[i] = (Message) getDao().persist(m);
            }

            //Test that we are not picking up archived messages 
            results = getDao().retrieve(recipientQuery);
            assertEquals(messages.length, results.size());
        } finally {
            getDao().delete(getDao().retrieve(Message.class));
        }
    }

    /**
     * Test of retrieveBySender method, of class HibernateMessageDao.
     */
    public void testRetrieveBySender() throws Exception {
        logger.info("retrieveBySender");
        
        Query senderQuery = Query.forClass(Message.class)
                .add(Expressions.eq("senderId", 13L))
                .add(Expressions.eq("enabled", true));
        
        Message[] messages = new Message[] {
            new Message(13L, 1L, "Test"),
            new Message(13L, 2L, "Test"),
            new Message(13L, 3L, "Test"),
            new Message(13L, 4L, "Test"),
            new Message(13L, 5L, "Test"),
            new Message(13L, 6L, "Test"),
            new Message(13L, 7L, "Test")
        };
        for (int i = 0; i < messages.length; i++) {
            messages[i] = (Message) getDao().persist((Message) messages[i]);
        }
        
        List<Message> results = (List<Message>) getDao().retrieve(senderQuery);
        assertEquals(messages.length, results.size());
        
         Message[] additional = new Message[] {
            new Message(14L, 1L, "Test"),
            new Message(14L, 2L, "Test"),
            new Message(14L, 3L, "Test"),
            new Message(14L, 4L, "Test"),
            new Message(14L, 5L, "Test"),
            new Message(14L, 6L, "Test"),
            new Message(14L, 7L, "Test")
        };
        for (int i = 0; i < additional.length; i++) {
            additional[i] = (Message) getDao().persist((Message) additional[i]);
        }
         
        //Test that we are not picking up other peoples messages 
        results = (List<Message>) getDao().retrieve(senderQuery);
        assertEquals(messages.length, results.size());
        
        
        Message[] archived = new Message[] {
            new Message(13L, 1L, "Test"),
            new Message(13L, 2L, "Test"),
            new Message(13L, 3L, "Test"),
            new Message(13L, 4L, "Test"),
            new Message(13L, 5L, "Test"),
            new Message(13L, 6L, "Test"),
            new Message(13L, 7L, "Test")
        };
        for (int i = 0; i < additional.length; i++) {
            Message m = (Message) archived[i];
            m.setEnabled(false);
            archived[i] = (Message) getDao().persist(m);
        }
         
        //Test that we are not picking up archived messages 
        results = (List<Message>) getDao().retrieve(senderQuery);
        assertEquals(messages.length, results.size());
    }

}
